<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Microsoft YaHei:300,300italic,400,400italic,700,700italic|Microsoft YaHei:300,300italic,400,400italic,700,700italic|Microsoft YaHei:300,300italic,400,400italic,700,700italic|Microsoft YaHei:300,300italic,400,400italic,700,700italic|Inziu Iosevka Slab SC:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Wavelet Transform," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="小波理论的数据压缩与去噪，小波变换的缺点，层次拟合，拟合出的数据不直观。 小波理论的基本概念及概述：http://blog.jobbole.com/101976/离散小波变换：https://www.wikiwand.com/zh-hans/%E9%9B%A2%E6%95%A3%E5%B0%8F%E6%B3%A2%E8%AE%8A%E6%8F%9B连续小波变换：https://wapbaike.b">
<meta name="keywords" content="Wavelet Transform">
<meta property="og:type" content="article">
<meta property="og:title" content="Wavelet Transform">
<meta property="og:url" content="http://idmk.oschina.io/2017/10/23/Wavelet-Transform/index.html">
<meta property="og:site_name" content="苦舟">
<meta property="og:description" content="小波理论的数据压缩与去噪，小波变换的缺点，层次拟合，拟合出的数据不直观。 小波理论的基本概念及概述：http://blog.jobbole.com/101976/离散小波变换：https://www.wikiwand.com/zh-hans/%E9%9B%A2%E6%95%A3%E5%B0%8F%E6%B3%A2%E8%AE%8A%E6%8F%9B连续小波变换：https://wapbaike.b">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://idmk.oschina.io/2017/10/23/Wavelet-Transform/assets/markdown-img-paste-20171023092042281.png">
<meta property="og:updated_time" content="2017-11-22T15:33:54.705Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Wavelet Transform">
<meta name="twitter:description" content="小波理论的数据压缩与去噪，小波变换的缺点，层次拟合，拟合出的数据不直观。 小波理论的基本概念及概述：http://blog.jobbole.com/101976/离散小波变换：https://www.wikiwand.com/zh-hans/%E9%9B%A2%E6%95%A3%E5%B0%8F%E6%B3%A2%E8%AE%8A%E6%8F%9B连续小波变换：https://wapbaike.b">
<meta name="twitter:image" content="http://idmk.oschina.io/2017/10/23/Wavelet-Transform/assets/markdown-img-paste-20171023092042281.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    sidebar: {"position":"left","display":"hide","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://idmk.oschina.io/2017/10/23/Wavelet-Transform/"/>





  <title>Wavelet Transform | 苦舟</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苦舟</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">学海无涯，吾将上下求索。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://idmk.oschina.io/2017/10/23/Wavelet-Transform/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="东木金">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苦舟">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Wavelet Transform</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-23T09:00:31+08:00">
                2017-10-23
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Mathematical-Analysis/" itemprop="url" rel="index">
                    <span itemprop="name">Mathematical Analysis</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>小波理论的数据压缩与去噪，小波变换的缺点，层次拟合，拟合出的数据不直观。</p>
<p>小波理论的基本概念及概述：<a href="http://blog.jobbole.com/101976/" target="_blank" rel="external">http://blog.jobbole.com/101976/</a><br>离散小波变换：<a href="https://www.wikiwand.com/zh-hans/%E9%9B%A2%E6%95%A3%E5%B0%8F%E6%B3%A2%E8%AE%8A%E6%8F%9B" target="_blank" rel="external">https://www.wikiwand.com/zh-hans/%E9%9B%A2%E6%95%A3%E5%B0%8F%E6%B3%A2%E8%AE%8A%E6%8F%9B</a><br>连续小波变换：<a href="https://wapbaike.baidu.com/item/%E8%BF%9E%E7%BB%AD%E5%B0%8F%E6%B3%A2%E5%8F%98%E6%8D%A2" target="_blank" rel="external">https://wapbaike.baidu.com/item/%E8%BF%9E%E7%BB%AD%E5%B0%8F%E6%B3%A2%E5%8F%98%E6%8D%A2</a></p>
<a id="more"></a>
<h2 id="小波理论的基本概念及概述"><a href="#小波理论的基本概念及概述" class="headerlink" title="小波理论的基本概念及概述"></a>小波理论的基本概念及概述</h2><h3 id="变换…啥？"><a href="#变换…啥？" class="headerlink" title="变换…啥？"></a>变换…啥？</h3><p>为了获取在原始信号中不易获得的信息，往往要对信号进行数学变换。以下篇幅均假定时域内信号为原始信号，经过数学变换后的信号为处理信号。<br>可用的变换有很多种，其中，傅立叶变换大概是目前最流行的。</p>
<p>实际中，多数信号的原始形式都是时域信号，也即不论如何测得的，信号总是关于时间的函数。换言之，绘制信号的图形时，一个轴代表时间（自变量），另一轴代表信号幅值（因变量）。在时域内作图，便可得到信号的时 - 幅表示。在多数信号处理有关的应用场景中，这种表示并不是最好的表示。<br>很多时候，最易分辨的信息往往隐藏在信号的频率成分中。信号的频谱是指信号中的频率分量（或谱分量），其表示的是信号中存在哪些频率成分。</p>
<p>直觉上，我们都知道频率是跟事物的变化率有关的量。如果一样东西（专业术语应该为数学量或物理量）变化得很快，则它的频率就高；变换得慢，或者说变化得很平滑，则它的频率就低。如果该量保持不变，则其频率为零，或者说没有频率。<br>频率用“循环次数 / 秒”，或者用更常用的“赫兹”来衡量。</p>
<p>那么怎样测量频率，或者说怎样得到一个信号中所含的频率成分呢？答案是傅立叶变换 (FT)。对时域信号做傅立叶变换，就会得到信号的频谱。也就是说，此时我们绘制信号图形的话，一个轴是频率，另一个轴是频率分量的幅值。所得图像将告诉我们信号中包含的各种频率成分分别有多少。</p>
<p>频率轴从零开始，直至正无穷。每个频率都对应一个幅值。例如，如果我们对房间所用的电流信号做傅立叶变换，频谱图中在 50Hz 处会出现尖峰，其它频率对应的幅值则为零，因为信号中只包含了 50Hz 的频率分量。然而，很少有信号的傅立叶变换是如此简单的。实际中的信号大都包含多个频率分量。50Hz 信号的傅立叶变换如下图所示：<br><img src="assets/markdown-img-paste-20171023092042281.png" alt=""><br>注意，图 1.4 给出了上下两张图，下图显示的其实是上图的前半部分。这是因为实值信号的频谱图是左右对称的，这点暂时不理解也无妨。上图能够看出这一特性。不过，由于后一半对称部分只不过是前一半图形的镜像，并未提供额外信息，因此，这部分经常不画出来。下文中出现的多数频谱图，我将只绘出前半部分。</p>
<h3 id="为什么需要频率信息？"><a href="#为什么需要频率信息？" class="headerlink" title="为什么需要频率信息？"></a>为什么需要频率信息？</h3><p>通常，一些在时域中不易看出的信息很容易在频域中观察到。</p>
<p>看一个生物信号的例子。设想我们正在观察一个心电信号。心脏专家一般都熟知典型的健康人心电图的形状。与这些典型形状存在显著偏差往往是疾病的征兆。<br>一些病征在时域表示的心电信号中并不明显。过去，心脏专家一般用记录在磁带上的时域心电图来分析心电信号。最近，新型的数字心电记录仪 / 分析仪可以利用心电图的频域信息来判断病征是否存在。对心电信号的频率成分进行分析能使他们更容易的诊断病情。</p>
<p>尽管傅立叶变换可能是使用最多的（特别在电气工程领域），但它并不是唯一的变换。许多其他的变换也常为工程师和数学家们所用，如希尔伯特变换、短时傅立叶变换（下文会有更多介绍）、魏格纳分布和雷登变换，当然还有教程的主角——小波变换，而这些也仅是工程师和数学家们所用变换中的一小部分。每种变换都有其应用领域，也有其优缺点，小波变换也不例外。</p>
<p><strong>需要同时知道时间和频率信息</strong><br>为了更好地理解为什么需要小波变换，我们需要更深刻地认识傅立叶变换。傅立叶变换是一种可逆变换，即它允许原始信号和处理信号之间互相变换。但是，在任意时刻只有一种信号形式是可用的。也就是说，在时域信号中不包含频率信息，而经过傅里叶变换后的信号则不包含任何时间信息。说到这，头脑里很自然地会提出一个问题，为什么需要同时知道时间和频率信息呢？<br>我们马上就会知道，答案是具体问题具体分析。回想一下，傅立叶变换给出了信号中的频率信息，即它可以告诉我们原始信号包含各个频率成分到底有多少，但是并未告诉我们某个频率信号何时出现。对于所谓的平稳信号，这些信息并不需要。<br>让我们进一步探讨一下平稳的概念，因为它在信号分析中具有重要意义。如果信号中的频率分量不随时间变化，则称这类信号为平稳信号。平稳信号中的频率分量一直保持不变，那么，自然无需知道频率分量是何时出现的，因为所有的频率分量出现在信号的每一刻！！！</p>
<p>注意，高频分量的幅值比低频分量大，这是因为高频信号（300ms）比低频信号（200ms）持续时间更长。（频率分量幅值的精确值并不重要）。傅立叶变换仅能给出信号中包含哪些频率分量，仅此而已。</p>
<p>如果我们仅关心信号中包含哪些频率分量而不关心它们出现的时间，傅立叶变换仍可用于处理非平稳信号。但是，如果我们想知道频率分量出现的确切时间（区间），傅立叶变换就不再适用了。</p>
<p>实际应用中，由于平稳的和非平稳的信号都很多，很那将二者区分开来。例如，几乎所有的生物信号都是非平稳的，包括广为人知的心电图 (ECG)、脑电图 (EEG) 和肌电图 (EMG)。</p>
<h3 id="终极解决方案：小波变换"><a href="#终极解决方案：小波变换" class="headerlink" title="终极解决方案：小波变换"></a>终极解决方案：小波变换</h3><p>当需要对频谱分量进行时间定位时，我们就需要一个可以给出信号时 - 频表示的变换。<br>小波变换是这种类型的变换，它提供了信号的时频表示（还有一些变换也可给出这些信息，如短时傅立叶变化，魏格纳分布等等）。</p>
<h3 id="滤波是如何奏效"><a href="#滤波是如何奏效" class="headerlink" title="滤波是如何奏效"></a>滤波是如何奏效</h3><p>这里解释一下滤波是如何奏效的：设想我们有一个信号，其中频率最高的分量为 1000Hz。第一步，我们通过高通和低通滤波器把信号分成两个信号（滤波器必须满足某些特定的条件，即容许条件），结果得到了同一信号的两个部分，0-500Hz 的部分（低通部分）和 500-1000Hz 的部分（高通部分）。<br>然后，我们可以拿其中一部分（通常是低通部分）或者二部分，然后对每一部分继续进行相同的操作。这个过程叫做分解。<br>假设我们拿低频部分做了处理，现在我们就有了 3 组数据，分别为信号在 0-250Hz，250-500Hz 和 500-1000Hz 的部分。<br>然后再对低通部分的信号继续做高通和低通滤波处理；现在我们就有了 4 组数据，分别为 0-125Hz，125-250Hz，250-500Hz 和 500-1000Hz。我们持续进行这个过程，直到将信号分解到一个预先定义的水平。这样我们就有了一系列信号，这些信号实际上都来自相同的信号，但是每一个都对应不同的频带。我们知道每个信号对应的频段，如果我们将这些信号放在一起画出三维图，一个轴表示时间，频率在另外一个轴上，幅度在第三个轴上。这幅图会告诉我们各个频率出现哪些时刻（这里有一个问题，叫做“不确定性原理”，即我们不能精确地知道哪个频率出现在哪些时间点，我们仅能知道某一频段出现在哪一时间区间内，后文中将有更多介绍）。</p>
<p>时 - 频平面内的一个确定的点上，信号的频率和时间信息不能同时知道。换句话说：在任一时刻，我们无法确定存在哪个频谱分量。我们最多只能做到，在一个给定的时间区间内存在哪些频谱分量。这是一个分辨率的问题，也是研究者们从快速傅立叶变换 (STFT) 切换到小波变换 (WT) 的主要原因。快速傅立叶变换的分辨率随时间是固定不变的，而小波变换则能给出可变的分辨率：</p>
<h2 id="离散小波变换"><a href="#离散小波变换" class="headerlink" title="离散小波变换"></a>离散小波变换</h2><p>离散小波变换（Discrete Wavelet Transform）在数值分析和时频分析中很有用。第一个离散小波变换由匈牙利数学家发明，离散小波转换顾名思义就是离散的输入以及离散的输出，但是这里并没有一个简单而明确的公式来表示输入及输出的关系，只能以阶层式架构来表示。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Wavelet-Transform/" rel="tag"># Wavelet Transform</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/10/13/Reviewing-and-Comparing-Algorithms-for-Detecting-this-Important-ECG-Waveform/" rel="next" title="Reviewing and Comparing Algorithms for Detecting This Important ECG Waveform">
                <i class="fa fa-chevron-left"></i> Reviewing and Comparing Algorithms for Detecting This Important ECG Waveform
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/10/28/Guidelines-For-Deep-Learning-Skills/" rel="prev" title="Guidelines for Deep Learning Skills">
                Guidelines for Deep Learning Skills <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/avatar.jpg"
               alt="东木金" />
          <p class="site-author-name" itemprop="name">东木金</p>
           
              <p class="site-description motion-element" itemprop="description">正在学习机器学习，希望能变得很强！</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">162</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">18</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">42</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/bdmk" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                    
                      GitHub
                    
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="mailto:catcherchan94@outlook.com" target="_blank" title="E-Mail">
                  
                    <i class="fa fa-fw fa-envelope"></i>
                  
                    
                      E-Mail
                    
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#小波理论的基本概念及概述"><span class="nav-number">1.</span> <span class="nav-text">小波理论的基本概念及概述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#变换…啥？"><span class="nav-number">1.1.</span> <span class="nav-text">变换…啥？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#为什么需要频率信息？"><span class="nav-number">1.2.</span> <span class="nav-text">为什么需要频率信息？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#终极解决方案：小波变换"><span class="nav-number">1.3.</span> <span class="nav-text">终极解决方案：小波变换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#滤波是如何奏效"><span class="nav-number">1.4.</span> <span class="nav-text">滤波是如何奏效</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#离散小波变换"><span class="nav-number">2.</span> <span class="nav-text">离散小波变换</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2017 - 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">东木金</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Gemini
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.2"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  






  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'manual') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
